package service

import (
	"context"
	"time"

	"gitee.com/thewalkers/refactoring-code/model"
)

// PaycheckService acts as a struct for injecting an implementation of PaycheckRepository
// for use in service methods
type PaycheckService struct {
	PaycheckRepository model.PaycheckRepository
}

// PAConfig will hold repository that will eventually be injected into this
// service layer
type PAConfig struct {
	PaycheckRespository model.PaycheckRepository
}

// NewPaymentService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewPayCheckService(c *PAConfig) model.PaycheckService {
	return &PaycheckService{
		PaycheckRepository: c.PaycheckRespository,
	}
}

// Create function return paycheck
func (p *PaycheckService) Create(ctx context.Context, payment *model.Paycheck) error {
	err := p.PaycheckRepository.Create(ctx, payment)
	return err
}

// Update function is to update paycheck
func (p *PaycheckService) Update(ctx context.Context, payment *model.Paycheck) error {
	err := p.PaycheckRepository.Update(ctx, payment)
	return err
}

func (p *PaycheckService) SetGrossPay(ctx context.Context, id int64, grossPay float64) error {
	paycheck := &model.Paycheck{
		ID:       id,
		GrossPay: grossPay,
	}

	return p.PaycheckRepository.Update(ctx, paycheck)
}

func (p *PaycheckService) SetDeductions(ctx context.Context, id int64, deduction float64) error {
	paycheck := &model.Paycheck{
		ID:        id,
		Deduction: deduction,
	}

	return p.PaycheckRepository.Update(ctx, paycheck)
}

func (p *PaycheckService) SetNetPay(ctx context.Context, id int64, netPay float64) error {
	paycheck := &model.Paycheck{
		ID:     id,
		NetPay: netPay,
	}

	return p.PaycheckRepository.Update(ctx, paycheck)
}

func (p *PaycheckService) Get(ctx context.Context, id int64) (*model.Paycheck, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck, err
}

func (p *PaycheckService) GetPayPeriodEndDate(ctx context.Context, id int64) (time.Time, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck.EndTime, err
}

func (p *PaycheckService) GetPayPeriodStartDate(ctx context.Context, id int64) (time.Time, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck.StartTime, err
}

func (p *PaycheckService) GetGrossPay(ctx context.Context, id int64) (float64, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck.GrossPay, err
}

func (p *PaycheckService) GetDeductions(ctx context.Context, id int64) (float64, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck.Deduction, err
}

func (p *PaycheckService) GetNetPay(ctx context.Context, id int64) (float64, error) {
	paycheck, err := p.PaycheckRepository.FindByID(ctx, id)
	return paycheck.NetPay, err
}
